home *** CD-ROM | disk | FTP | other *** search
- /*
- * bc.c
- *
- * Practical Algorithms for Image Analysis
- *
- * Copyright (c) 1997, 1998, 1999 MLMSoftwareGroup, LLC
- */
-
- /*
- * BC (background correction)
- *
- * Divides the input image by the reference image
- * (to correct for nonuniform background and shading), scales
- * the result and wrtes the image to the specified output file
- *
- */
-
- #include "bc.h"
-
- #define NOLOOP 0
- #define INDEX_SCALE 255
-
- #define ON 1
- #define OFF 0
-
- /*
- * global variables
- */
- extern char *optarg;
- extern int optind, opterr;
- extern short tiffInput; /* flag=0 if no ImageIn to set tags; else =1 */
-
- int BINARIZE = OFF;
-
- /*
- * usage of routine
- */
- void
- usage (char *progname)
- {
- progname = last_bs (progname);
- printf ("USAGE: %s in1img in2img outimg [-c clip][-b][-L]\n", progname);
- printf ("\n%s divides the first input image by the second input image\n", progname);
- printf ("(to correct for nonuniform background and shading), scales\n");
- printf ("the result and writes the image to the specified output file.\n\n");
- printf ("ARGUMENTS:\n");
- printf (" in1img: first input image filename (TIF)\n");
- printf (" in2img: second input image filename (TIF)\n");
- printf (" outimg: output image filename (TIF)\n\n");
- printf ("OPTIONS:\n");
- printf (" -c clip: clipping factor for normalized image 0-255(default=1)\n");
- printf (" -b: generate binarized output by conversion to integer\n");
- printf (" -L: print Software License for this module\n");
- exit (1);
- }
-
- /*
- * background correction
- * divide active image by reference image and scale to 255
- * reference: S. Inoue, "Video Microscopy", chapt. 10
- */
- void
- backgrd_correct (Image * imgIn, Image * imgRef, Image * imgOut, int clip_factor, int binarize)
- {
- int ix, iy;
- int max_x, max_y;
- float pixelIn, pixelRef;
- float norm_pix = (float) 0.0;
- float norm_pix_max = (float) 0.0;
- int j;
-
- max_y = ImageGetHeight (imgIn);
- max_x = ImageGetWidth (imgIn);
- for (ix = 0; ix < max_x; ix++) {
- for (iy = 0; iy < max_y; iy++) {
- pixelIn = (float) (getpixel (ix, iy, imgIn));
- pixelRef = (float) (getpixel (ix, iy, imgRef));
- pixelRef = (pixelRef + (float) 1 > (float) INDEX_SCALE) ? (float) INDEX_SCALE : pixelRef + (float) 1;
- if (pixelRef < 2.0)
- continue;
- if (clip_factor) {
- norm_pix = (pixelIn / pixelRef > (float) clip_factor) ? (float) clip_factor : pixelIn / pixelRef;
- if (binarize)
- setpixel (ix, iy, imgOut, (int) (INDEX_SCALE * (int) (norm_pix + 0.5)));
- else
- setpixel (ix, iy, imgOut, (int) norm_pix);
- }
- else { /*just get the maximum normalized pixel */
- norm_pix = pixelIn / pixelRef;
- if (norm_pix > norm_pix_max)
- norm_pix_max = norm_pix;
- }
- }
- }
- /*
- * if there was no clipping factor specified,
- * we just calculated the maximum normalized pixel value
- * now scale the image according to norm_pix_max
- */
- if (!clip_factor) {
- for (ix = 0; ix < max_x; ix++) {
- for (iy = 0; iy < max_y; iy++) {
- if (ix > 100 && iy > 100)
- j = 0;
- pixelIn = (float) (getpixel (ix, iy, imgIn));
- pixelRef = (float) (getpixel (ix, iy, imgRef));
- pixelRef = (pixelRef + (float) 1 > (float) INDEX_SCALE) ? (float) INDEX_SCALE : pixelRef + (float) 1;
- norm_pix = pixelIn / (pixelRef * norm_pix_max);
- if (binarize)
- setpixel (ix, iy, imgOut, (int) (INDEX_SCALE * (int) (norm_pix + 0.5)));
- else
- setpixel (ix, iy, imgOut, (norm_pix > 1.0 ? INDEX_SCALE : (int) (INDEX_SCALE * norm_pix)));
- }
- }
- }
- }
-
- void
- main (int argc, char *argv[])
- {
- Image *imgIn;
- Image *imgOut;
- Image *imgRef;
-
- int i_arg;
- int clip_factor = 0;
- int binarize = 0;
-
- /*
- * cmd line options
- */
- static char *optstring = "c:bL";
-
-
- /*
- * parse command line
- */
- optind = 4; /* set getopt to point to the 4th arg */
- opterr = ON; /* give error messages */
-
-
- if (argc < 4)
- usage (argv[0]);
-
- while ((i_arg = getopt (argc, argv, optstring)) != EOF) {
- switch (i_arg) {
- case 'b':
- printf ("\n...option %c: generate binary output\n", i_arg);
- binarize = 1;
- break;
- case 'c':
- if ((clip_factor = atoi (optarg)) > 255) {
- printf ("Clipping factor must be <= 255\n");
- printf ("Defaulting to calculated clipping factor\n");
- clip_factor = 0;
- }
- break;
- case 'L':
- print_sos_lic ();
- exit (0);
- default:
- printf ("\ngetopt: unknown condition encountered\n");
- exit (1);
- break;
- }
- }
-
- /*
- * Read input image
- */
- imgIn = ImageIn (argv[1]);
-
- if (imgIn->bps == 8 && imgIn->spp == 3) {
- printf ("Got RGB image!!!\nInput image must be Grayscale or B&W!!\n");
- exit (1);
- }
- /*
- * Read reference image
- */
- imgRef = ImageIn (argv[2]);
-
- if (ImageGetHeight (imgIn) != ImageGetHeight (imgRef) ||
- ImageGetWidth (imgIn) != ImageGetWidth (imgRef)) {
- printf ("Input and Reference images cannot have different sizes\n");
- exit (1);
- }
-
- /*
- * Allocate memory for output image
- */
- imgOut = ImageAlloc ((long) ImageGetHeight (imgIn), (long) ImageGetWidth (imgIn), (long) BPS);
-
- /*
- * perform background illumination correction
- */
- backgrd_correct (imgIn, imgRef, imgOut, clip_factor, binarize);
-
- /*
- * reset tiffInput so that we write a grayscale file (i.e tags are not copied)
- */
- tiffInput = 0;
-
- /*
- * Write the output image
- */
- ImageOut (argv[3], imgOut);
- }
-